package com.longxi.tianjintianbao.ui.presenter.assetscheck;

import android.text.TextUtils;
import android.view.View;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponseWebPd;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.assetscheck.IAssetsWebServiceAtView;
import com.longxi.tianjintianbao.utils.AESUtil;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.widget.AssetsWebLoginDialog;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/12/13
 */
public class AssetsWebServiceAtPresenter extends BasePresenter<IAssetsWebServiceAtView> {

    public AssetsWebServiceAtPresenter(BaseActivity context) {
        super(context);
    }

    public void isShowLoginBtn(){

        if(!MyApplication.isPDLogin){
            showLoginDialog();
            getView().getLlLogin().setVisibility(View.VISIBLE);
            getView().getLlZCPD().setVisibility(View.GONE);
        }else{
            getView().getLlLogin().setVisibility(View.GONE);
            getView().getLlZCPD().setVisibility(View.VISIBLE);
        }
    }

    AssetsWebLoginDialog.Builder loginBuilder;
    private void showLoginDialog(){
        if(loginBuilder != null){
            loginBuilder.show();
            loginBuilder.setUsernameAndPassword(getSp("USERNAME"), getSp("PASSWORD"));
            return;
        }

        loginBuilder = new AssetsWebLoginDialog.Builder(mContext);
        loginBuilder.setTitle("登录盘点系统")
        .setListener(new AssetsWebLoginDialog.OnLoginListener() {
            @Override
            public void onLogin(String username, String password) {
                login(username, password);
            }
        })
        .create()
        .show();
        loginBuilder.setUsernameAndPassword(getSp("USERNAME"), getSp("PASSWORD"));
    }

    /**
     * 登录用
     */
    public void login(String username, String password){
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
            MyUtils.showToast("请输入用户名或密码");
            return;
        }

        String usernameApassword = "";
        try {
            usernameApassword = username + ",," + AESUtil.encrypt(password, "1234567890123456");
        } catch (Exception e) {
            e.printStackTrace();
        }

        mContext.showWaitingDialog("正在登录");
        JSONObject object = new JSONObject();
        try {
            object.put("type", "validate");
            object.put("validate", usernameApassword);
            object.put("billtype", "");
            object.put("billcode", "");
            object.put("billdata", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().validate(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponseWebPd>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponseWebPd result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }

                if(result.getData().contains("ERROR")){
                    MyUtils.showToast(result.getData());
                    return;
                }

                saveSp("USERNAME", username);
                saveSp("PASSWORD", password);

                MyApplication.username = username;
                MyApplication.password = password;

                MyApplication.isPDLogin = true;
                isShowLoginBtn();

                loginBuilder.dismiss();

            }
        });
    }

    public void setBar() {
        mContext.setTvLeft("返回");
        mContext.setTitle("资产盘点");
    }
}
